import 'dart:convert';

import 'package:dio/dio.dart';

enum HttpMethod { post, get }

typedef ParseObject = dynamic Function(dynamic originMap);

extension ListExt on List<dynamic> {
  List<T> parseObj<T>(ParseObject covert) {
    return List<T>.from(map(covert)).toList();
  }
}

extension ResultJsonExt on ResultJson? {
  // bool get requestIsSuccess =>
  //     this != null && this!.success == true && this!.errors.isEmpty;
  bool get requestIsSuccess => this != null && this!.code == 200;
}

abstract class BaseModel<T> {
  T fromJson(Map<String, dynamic> map);
}

class ResultJson1 {
  final bool success;
  final String errorMessage;
  final List<dynamic> errors;
  final dynamic data;

  ResultJson1(
      {required this.success,
      required this.errorMessage,
      required this.errors,
      required this.data});

  factory ResultJson1.fromJson(Map<String, dynamic> map) {
    return ResultJson1(
        success: map['success'],
        errorMessage: map['errorMessage'] ?? '',
        errors: map['errors'] ?? [],
        data: map['data']);
  }
}

class ResultJson {
  final int code;
  final String message;
  final dynamic data;

  ResultJson({required this.code, required this.message, required this.data});

  factory ResultJson.fromJson(Map<String, dynamic> map) {
    return ResultJson(
        code: map['code'], message: map['message'] ?? '', data: map['data']);
  }

  //打印
  void printDataJson() {
    print(jsonEncode(data));
  }

  //转数组
  List<dynamic> parseList() {
    return data as List<dynamic>;
  }

  List<T> dataCovertToList<T>(ParseObject covert) {
    return parseList().parseObj<T>(covert).toList();
  }

  @override
  String toString() {
    return '==========================================\n是否成功:${code == 200 ? '成功' : '失败'} data类型:${data.runtimeType} \n反馈消息:$message\n错误列表:\n请求数据:$data\n==========================================';
  }
}

abstract class BaseApi {
  static String host = 'http://192.168.0.108:9091';

  final String url;
  final HttpMethod httpMethod;
  final Map<String, dynamic>? header = null;
  final Map<String, dynamic> params = <String, dynamic>{};

  BaseApi(this.url, {this.httpMethod = HttpMethod.get});

  /// 配置dio配置参数

  Future<ResultJson?> request() async {
    // 配置option
    BaseOptions options = BaseOptions();

    ///请求header的配置
    options.baseUrl = host;
    // 配置请求头
    Map<String, dynamic> headers = {};
    if (header != null) {
      header?.forEach((key, value) {
        headers[key] = value;
      });
    }
    options.headers.addAll(headers);

    /// 设置超时时间
    options.connectTimeout = 30000;
    final response = await Dio(options).request(url,
        options: Options(method: method),
        queryParameters: params,
        data: params);
    if (response.statusCode == 200) {
      final data = response.data;
      return ResultJson.fromJson(data);
    }
    return null;
  }

  //请求方法,
  String get method => getMethod();

  String getMethod() {
    switch (httpMethod) {
      case HttpMethod.get:
        return 'GET';
      default:
        return "POST";
    }
  }
}
